package com.jingsheng.jingshengcity.mvp.ui.activity.wallet;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;

import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.UiUtils;
import com.jingsheng.jingshengcity.R;
import com.jingsheng.jingshengcity.di.component.DaggerBindBankCardComponent;
import com.jingsheng.jingshengcity.di.module.BindBankCardModule;
import com.jingsheng.jingshengcity.mvp.contract.BindBankCardContract;
import com.jingsheng.jingshengcity.mvp.presenter.BindBankCardPresenter;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Consumer;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * Created by Administrator on 2017/5/13.
 */

public class BindBankCardActivity extends BaseActivity<BindBankCardPresenter> implements BindBankCardContract.View {

    @NonNull
    @BindView(R.id.et_name)
    EditText mEtName;
    @NonNull
    @BindView(R.id.et_card_no)
    EditText mEtCardNo;
    @NonNull
    @BindView(R.id.btn_next)
    Button mBtnNext;

    @Override
    public void setupActivityComponent(AppComponent appComponent) {
        DaggerBindBankCardComponent
                .builder()
                .appComponent(appComponent)
                .bindBankCardModule(new BindBankCardModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView() {
        setTitle("银行卡绑定");
        return R.layout.activity_bind_bank_card;
    }

    @Override
    public void initData() {

        Observable<CharSequence> observableName = RxTextView.textChanges(mEtName);
        Observable<CharSequence> observableNo = RxTextView.textChanges(mEtCardNo);

        Observable.combineLatest(observableName, observableNo, new BiFunction<CharSequence, CharSequence, Boolean>() {
            @Override
            public Boolean apply(@io.reactivex.annotations.NonNull CharSequence name, @io.reactivex.annotations.NonNull CharSequence no) throws Exception {
                if (!TextUtils.isEmpty(name) && !TextUtils.isEmpty(no))
                    return true;
                return false;
            }
        }).subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(@io.reactivex.annotations.NonNull Boolean aBoolean) throws Exception {
                if (aBoolean) {
                    mBtnNext.setEnabled(true);
                } else {
                    mBtnNext.setEnabled(false);
                }
            }
        });

        RxView.clicks(mBtnNext)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(@io.reactivex.annotations.NonNull Object o) throws Exception {
                        toNext();
                    }
                });

    }

    private void toNext() {
        Intent intent = new Intent(this, BindBankCardNextActivity.class);
        startActivity(intent);
    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        UiUtils.SnackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        UiUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public boolean useFragment() {
        return false;
    }
}